package org.aristotle.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aristotle.common.response.ResponseEntity;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.utils.AristotleBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.aristotle.user.model.entity.RolePermission;
import org.aristotle.user.service.IRolePermissionService;
import org.aristotle.user.model.vo.RolePermissionVO;

@RestController
@RequestMapping("rolePermission")
@Api(value="角色权限联系controller",tags={"角色权限联系操作接口"})
public class RolePermissionController {

    @Autowired
    private IRolePermissionService rolePermissionService;

    /*分页查询*/
    @GetMapping("page")
    @ApiOperation("分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "角色权限联系id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "roleId", paramType = "query", value = "角色Id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "permissionId", paramType = "query", value = "权限Id", dataType = "Long", required = false),
            @ApiImplicitParam(name = "current", paramType = "query", value = "页码", dataType = "Integer", required = false,defaultValue = "1"),
            @ApiImplicitParam(name = "size", paramType = "query", value = "页距", dataType = "Integer", required = false,defaultValue = "10")
    })
    public Result<Page<RolePermissionVO>> queryPageList(
            @RequestParam(name = "id",required = false) Long id,
            @RequestParam(name = "roleId",required = false) Long roleId,
            @RequestParam(name = "permissionId",required = false) Long permissionId,
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Page<RolePermissionVO> result = rolePermissionService.queryPageList(
            id,
            roleId,
            permissionId,
            current,size);
        return ResponseEntity.ok(result);
    }

    @GetMapping
    @ApiOperation("根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "query", value = "角色权限联系id", dataType = "Long", required = true)
    })
    public Result<?> queryById(@RequestParam(name = "id") Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        RolePermissionVO result = rolePermissionService.getById(id);
        if (result == null) {
            return ResponseEntity.notFound();
        }
        return ResponseEntity.ok(result);
    }

    @GetMapping("list")
    @ApiOperation("获取列表")
    public Result<?> queryList() {
        return ResponseEntity.ok(AristotleBeanUtils.copyListProperties(rolePermissionService.list(),RolePermissionVO::new));
    }

    @PostMapping
    @ApiOperation("添加")
    public Result<?> add(@RequestBody RolePermissionVO rolePermissionVO) {
        if (
            rolePermissionVO.getRoleId() == null
            || rolePermissionVO.getPermissionId() == null
        ) {
            return ResponseEntity.badRequest();
        }
        RolePermission rolePermission = AristotleBeanUtils.transitionBean(rolePermissionVO, RolePermission::new);
        rolePermission.setId(null);
        rolePermissionService.save(rolePermission);
        return ResponseEntity.addOk(rolePermissionVO);
    }

    @PutMapping
    @ApiOperation("修改")
    public Result<?> edit(@RequestBody RolePermissionVO rolePermissionVO) {
        if (rolePermissionVO.getId() == null) {
            return ResponseEntity.badRequest();
        }
        if (
            rolePermissionVO.getRoleId() == null
            || rolePermissionVO.getPermissionId() == null
        ) {
            return ResponseEntity.badRequest();
        }
        RolePermission rolePermission = AristotleBeanUtils.transitionBean(rolePermissionVO, RolePermission::new);
        rolePermissionService.updateById(rolePermission);
        return ResponseEntity.updateOk(rolePermissionVO);
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", paramType = "path", value = "角色权限联系id", dataType = "Long", required = true)
    })
    public Result<?> delete(@PathVariable Long id) {
        if (id == null) {
            return ResponseEntity.badRequest();
        }
        rolePermissionService.removeById(id);
        return ResponseEntity.deleteOk(id);
    }

}
